﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DAL;
using System.Threading;

/// <summary>
///Task 的摘要说明
/// </summary>
public class Task
{
    static Task()
    {
        Thread thread = new Thread(ImportNews);
        thread.Priority = ThreadPriority.Lowest;
        thread.Start();
    }

    //导入新闻
    static void ImportNews()
    {
        while (true)
        {
            try
            {
                CMSModel.CMSEntities dal = DAL.DBManager.GetCmsEmtity();
                var data = dal.CPTypeMatching.ToArray();
                StockInfoDAL s = new StockInfoDAL();
                NewsImportBLL importBll = new NewsImportBLL();
                foreach (var c in data)
                {
                    var newsLst = s.NewsListByTypeId(c.CpTypeId, c.UTime);
                    for (int i = newsLst.Rows.Count - 1; i >= 0; i--)
                    {
                        importBll.Import((long)newsLst.Rows[i]["ob_textid_0022"], c.LocalTypeId);
                        c.UTime = Tools.GetDateTime(newsLst.Rows[i]["ob_rectime_0022"]);
                        dal.SaveChanges();
                    }
                }
            }
            catch
            {
            }
            //休眠1小时
            Thread.Sleep(60 * 20 * 1000);
        }
    }

    public void import()
    {
        CMSModel.CMSEntities dal = DAL.DBManager.GetCmsEmtity();
        var data = dal.CPTypeMatching.ToArray();
        StockInfoDAL s = new StockInfoDAL();
        NewsImportBLL importBll = new NewsImportBLL();
        foreach (var c in data)
        {
            var newsLst = s.NewsListByTypeId(c.CpTypeId, c.UTime);
            for (int i = newsLst.Rows.Count - 1; i >= 0; i--)
            {
                importBll.Import((long)newsLst.Rows[i]["ob_textid_0022"], c.LocalTypeId);
                c.UTime = Tools.GetDateTime(newsLst.Rows[i]["ob_rectime_0022"]);
                dal.SaveChanges();
            }
        }
    }
}